/**
 * api 接口返回值对象
 * by @author lms 2022.07.15
 */
package ms.core.common;

import java.util.HashMap;
import java.util.Map;

import lombok.Data;
import ms.core.common.consts.SysErr;
import ms.core.common.utils.CodeTableUtils;
import ms.core.tool.JsonTool;
import ms.core.tool.StrTool;

@Data
public class ApiResult {
	private static final String SUCCESS = "success";
	private static final String FAILURE = "failure";
	
	private String result;
	private String code;
	private String msg;
	private Object data;
	private Map<String, Object> ext;

	public ApiResult() {}

	public ApiResult(boolean success) {
		result = success ? SUCCESS : FAILURE;
		code = success ? SysErr.CODE_SUCCESS : SysErr.CODE_FAILURE;
		msg = CodeTableUtils.codeOfMsg(code);
	}

	public ApiResult(boolean success, String code) {
		result = success ? SUCCESS : FAILURE;
		this.code = code;
		this.msg = CodeTableUtils.codeOfMsg(code);
	}

	/**
	 * 自定义消息
	 * 
	 * @param success
	 * @param code
	 * @param msg
	 */
	public ApiResult(boolean success, String code, String msg) {
		result = success ? SUCCESS : FAILURE;
		this.code = code;

		if (!StrTool.isBlank(msg)) this.msg = msg;
		else this.msg = CodeTableUtils.codeOfMsg(code);
	}

	/**
	 * 自定义消息+数据
	 * 
	 * @param success
	 * @param code
	 * @param msg
	 * @param data
	 */
	public ApiResult(boolean success, String code, String msg, Object data) {
		result = success ? SUCCESS : FAILURE;
		this.code = code;

		if (!StrTool.isBlank(msg)) this.msg = msg;
		else this.msg = CodeTableUtils.codeOfMsg(code);

		this.data = data;
	}

	public ApiResult setData(Object data) {
		this.data = data;
		return this;
	}

    public ApiResult put(String key, Object value) {
        if (ext==null) ext = new HashMap<String, Object>();
        ext.put(key, value);
        return this;
    }
    
    public Object get(String key) {
        if (ext == null) return null;
        return ext.get(key);
    }
    
    public ApiResult remove(String key) {
        if (ext == null) return this;

        ext.remove(key);
        return this;
    }

    /**
     * 扩展分页信息
     */
    public ApiResult setPage(int page) {
        this.put("page", page);
        return this;
    }

    public ApiResult setPageSize(int pagesize) {
        this.put("pagesize", pagesize);
        return this;
    }

    public ApiResult setPages(int pages) {
        this.put("pages", pages);
        return this;
    }

    public ApiResult setRows(int rows) {
        this.put("rows", rows);
        return this;
    }

    public <T> ApiResult setPage(Page<T> pg) {
    	if (pg==null) return this;

    	this.data = pg.getData();
        this.put("page", pg.getPage());
        this.put("pagesize", pg.getPageSize());
        this.put("pages", pg.getPages());
        this.put("rows", pg.getRows());
        return this;
    }

    public ApiResult setPage(int page, int pagesize, int pages, int rows) {
        this.put("page", page);
        this.put("pagesize", pagesize);
        this.put("pages", pages);
        this.put("rows", rows);
        return this;
    }

    @Override
    public String toString() {
        Map<String, Object> map = JsonTool.objectToMap(this, true);
        if (ext != null) {
        	map.remove("ext");
        	map.putAll(ext);
            ext.clear();
        }
        return JsonTool.objectToJson(map);
    }
}
